package com.ruoyi.logistic.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.logistic.domain.NetworkSalesmanSettlementClientCollect;
import com.ruoyi.logistic.domain.NetworkSalesmanSettlementClientDetail;
import com.ruoyi.logistic.domain.PageDate;
import com.ruoyi.logistic.domain.vo.NetworkSalesmanSettlementClientCollectVo;
import com.ruoyi.logistic.domain.vo.NetworkSalesmanSettlementClientDetailVo;
import com.ruoyi.logistic.mapper.NetworkSalesmanSettlementClientCollectMapper;
import com.ruoyi.logistic.mapper.NetworkSalesmanSettlementClientDetailMapper;
import com.ruoyi.logistic.service.NetworkSalesmanSettlementClientDetailService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NetworkSalesmanSettlementClientDetailServiceImpl implements NetworkSalesmanSettlementClientDetailService {
    @Autowired
    private NetworkSalesmanSettlementClientDetailMapper networkSalesmanSettlementClientDetailMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private NetworkSalesmanSettlementClientCollectMapper networkSalesmanSettlementClientCollectMapper;
    @Override
    public List<NetworkSalesmanSettlementClientDetail> selectAll(PageDate pageDate) {
        PageHelper.startPage(pageDate);
        return networkSalesmanSettlementClientDetailMapper.selectAll();
    }

    @Override
    public Boolean insert(NetworkSalesmanSettlementClientDetail salesmanSettlementClientDetail) {
        salesmanSettlementClientDetail.setCreateBy(SecurityUtils.getUserId());
        return networkSalesmanSettlementClientDetailMapper.insert(salesmanSettlementClientDetail);
    }

    @Override
    public Boolean update(NetworkSalesmanSettlementClientDetail salesmanSettlementClientDetail) {
        salesmanSettlementClientDetail.setUpdateBy(SecurityUtils.getUserId());
        return networkSalesmanSettlementClientDetailMapper.update(salesmanSettlementClientDetail);
    }

    @Override
    public Boolean deleteById(Integer id) {
        return networkSalesmanSettlementClientDetailMapper.deleteById(id);
    }

    @Override
    public List<NetworkSalesmanSettlementClientDetail> search(NetworkSalesmanSettlementClientDetailVo vo) {
        PageHelper.startPage(vo.getPageDate());
        List<NetworkSalesmanSettlementClientDetail> detailList = networkSalesmanSettlementClientDetailMapper.search(vo);
        for (NetworkSalesmanSettlementClientDetail detail : detailList){
            detail.setSysUser(sysUserMapper.selectUserById(detail.getUpdateBy()));
        }
        return detailList;
    }

    @Override
    public List<NetworkSalesmanSettlementClientCollect> collect(NetworkSalesmanSettlementClientCollectVo vo) {
        PageHelper.startPage(vo.getPageDate());
        List<NetworkSalesmanSettlementClientCollect> list = networkSalesmanSettlementClientCollectMapper.collect(vo);
        assert list != null;
        for (NetworkSalesmanSettlementClientCollect clientCollect : list){
            if (vo.getCollectType() == 2){
                clientCollect.setCollectDate(vo.getStartTime() + "至" + vo.getEndTime());
            }
        }
        return list;
    }
}
